package com.jinzhi.jzweb.controller.draw;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jinzhi.api.exception.IFastApiException;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.type.ApiEnumError;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.TrackDrawDO;
import com.jinzhi.jzweb.domain.TrackDrawVO;
import com.jinzhi.jzweb.service.draw.FuZhouDrawService;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: mcf
 * @CreateTime: 2024/10/31
 * @Description: 福州文教赛道抽签
 *
 * -------------------抽签规则----------------------
 * 32个学校，66个选手（有一个学校两支队伍）
 * 抽签结果显示
 * 1选手比赛B赛道
 * 2选手去对应C赛道
 * B赛道1～16上午比赛，17到33下午比赛（上午比完封闭不出考场）
 * C赛道1～16上午比赛，17到33下午比赛（同样封闭等待）
 *
 */
@Controller
@RequestMapping("/jzweb/fuzhouDraw")
public class FuZhouDrawController {

    @Autowired
    private FuZhouDrawService fuZhouDrawService;

    /**
     * 抽签页面
     * @return
     */
    @GetMapping()
    @RequiresPermissions("jzweb:fuzhouDraw:fuzhouDraw")
    String TrackDraw(){
        return "jzweb/fuzhouDraw/fuzhouDraw";
    }

    /**
     * 抽签页面
     * @return
     */
    @GetMapping("/school")
    String TrackSchoolDraw(){
        return "jzweb/trackDrawConfig/trackDrawConfig";
    }

    /**
     * 抽签接口
     * @return
     */
    @ResponseBody
    @GetMapping("/draw")
    public Result<?> draw(){

        return fuZhouDrawService.draw();
    }


    /**
     * 查询上次的抽签结果
     */
    @ResponseBody
    @GetMapping("/trackList")
    public Result<TrackDrawDO> trackList(){

        return Result.ok(fuZhouDrawService.trackList());
    }


    /**
     * 导出抽签结果信息
     *
     * @param response
     */
    @RequestMapping("/downloadImportTemplate")
    @ResponseBody
    public void downloadImportTemplate(HttpServletRequest request, HttpServletResponse response) {
        Wrapper<TrackDrawDO> wrapper = new EntityWrapper<>();
        wrapper.orderBy(true,"sort");
        List<TrackDrawDO> trackDrawDOS = fuZhouDrawService.selectList(wrapper);
        if (trackDrawDOS != null && trackDrawDOS.size() > 0) {
            //导出操作
            EasyPoiUtil.exportExcel(trackDrawDOS, "第二十届福州市职业院校技能大赛赛道抽签信息", "第二十届福州市职业院校技能大赛赛道抽签信息", TrackDrawDO.class,  "第二十届福州市职业院校技能大赛赛道抽签信息", response);
        } else {
            throw new IFastApiException(ApiEnumError.NOTBATCHSTU.getCodeStr());
        }
    }


    /**
     * 清空抽签结果
     * @return
     */
    @ResponseBody
    @GetMapping("/empty")
    public Result<?> empty(){

        return Result.ok(fuZhouDrawService.delete(new EntityWrapper<TrackDrawDO>()));
    }


    /**
     * 抽签接口
     * @return
     */
    @ResponseBody
    @GetMapping("/schoolDraw")
    public Result<?> schoolDraw(){

        return fuZhouDrawService.schoolDraw();
    }


    /**
     * 查询上次的抽签结果
     */
    @ResponseBody
    @GetMapping("/trackSchoolList")
    public Result<TrackDrawDO> trackSchoolList(){

        return Result.ok(fuZhouDrawService.trackSchoolList());
    }


    /**
     * 导出抽签结果信息
     *
     * @param response
     */
    @RequestMapping("/downloadImportTemplateSchool")
    @ResponseBody
    public void downloadImportTemplateSchool(HttpServletRequest request, HttpServletResponse response) {
        Wrapper<TrackDrawDO> wrapper = new EntityWrapper<>();
        wrapper.orderBy(true,"sort");
        List<TrackDrawDO> trackDrawDOS = fuZhouDrawService.selectList(wrapper);
        List<TrackDrawVO> list = new LinkedList<>();
        if(null!=trackDrawDOS && !trackDrawDOS.isEmpty()){
            for (TrackDrawDO trackDrawDO : trackDrawDOS) {
                TrackDrawVO trackDrawVO = new TrackDrawVO();
                BeanUtils.copyProperties(trackDrawDO,trackDrawVO);
                list.add(trackDrawVO);
            }
        }
        if (list != null && list.size() > 0) {
            //导出操作
            EasyPoiUtil.exportExcel(list, "抽签信息导出", "抽签信息导出", TrackDrawVO.class,  "抽签信息导出", response);
        } else {
            throw new IFastApiException(ApiEnumError.NOTBATCHSTU.getCodeStr());
        }
    }



}
